package cn.ffcs.utils;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 已弃用、使用连接池代替
 */
@Data
@Slf4j
@Component
public class FTPUtil {

    @Resource
    private FTPClientConfigure configure;

    private boolean connectServer(FTPClient ftpClient, String ip,int port,String user,String pwd){
        Boolean isSuccess=false;
        try {
            log.info("connect to FTP IP:"+configure.getHost() + ":" + configure.getPort());
            ftpClient.enterLocalPassiveMode();
            ftpClient.setDefaultPort(port);
            ftpClient.connect(ip);
            isSuccess = ftpClient.login(user,pwd);
        } catch (IOException e) {
            //isSuccess = false;
            e.printStackTrace();
            log.error("连接ftp服务器失败");
        }
        if(!isSuccess) {
            log.error("连接ftp服务器失败");
        }
        return isSuccess;
    }

    public boolean uploadFileList(String remotePath, List<File> fileList) throws IOException {
        FTPClient ftpClient = new FTPClient();
        boolean upload=true;
        FileInputStream fileInputStream=null;
        //connect to ftpServer
        if (connectServer(ftpClient, configure.getHost(),configure.getPort(),configure.getUsername(),configure.getPassword())){
            try {
                ftpClient.changeWorkingDirectory(remotePath);
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("UTF-8");
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();
                for (File fileItem:fileList
                ) {
                    fileInputStream=new FileInputStream(fileItem);
                    ftpClient.storeFile(fileItem.getName(),fileInputStream);
                }
            } catch (IOException e) {
                log.error("上传文件异常",e);
                upload=false;
            }finally {
                fileInputStream.close();
                ftpClient.disconnect();
            }
        }
        return upload;
    }

    public boolean uploadToFtp(String remotePath, String fileName, InputStream fileInputStream) throws IOException {
        FTPClient ftpClient = new FTPClient();
        boolean upload = true;
        //connect to ftpServer
        if (connectServer(ftpClient, configure.getHost(),configure.getPort(),configure.getUsername(),configure.getPassword())) {
            try {
                System.out.println(ftpClient.printWorkingDirectory());
                boolean s = ftpClient.changeWorkingDirectory(remotePath);
                System.out.println(s);
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("UTF-8");
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                //上传文件 参数：上传后的文件名，输入流
                upload = ftpClient.storeFile(fileName, fileInputStream);
            } catch (IOException e) {
                log.error("上传文件异常", e);
                upload = false;
            } finally {
                ftpClient.disconnect();
            }
        } else {
            upload = false;
        }
        return upload;
    }
}
